<template>
  <el-dialog v-model="dialogVisible" title="编辑主图" width="800">
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      label-width="100"
      class="demo-ruleForm"
      status-icon
    >
      <el-form-item label="图片" prop="name">
        <el-upload
          v-model:file-list="fileList"
          action="https://www.jqw2019.com/admin/api/v1/files"
          list-type="picture-card"
          :headers="headers"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :on-exceed="handleExceed"
          :on-success="handleSuccess"
          :limit="1"
        >
          <el-icon><Plus /></el-icon>
        </el-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit(ruleFormRef)">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
  <el-dialog v-model="dialogVisibleImg">
    <img w-full :src="dialogImageUrl" alt="Preview Image" />
  </el-dialog>
</template>

<script setup>
import { ref } from "vue";
import { TOKEN_KEY } from "@/enums/CacheEnum";
import { ElMessageBox } from "element-plus";
import serviceTypeAPI from "@/api/serviceType";

const headers = { Authorization: localStorage.getItem(TOKEN_KEY) };
const id = ref();
const dialogVisible = ref(false);
const init = (value) => {
  serviceTypeAPI
    .showServiceInfo(value)
    .then((res) => {
      if (res.banner) {
        fileList.value[0] = {
          url: res.banner,
        };
      }
    })
    .catch((err) => {});
  dialogVisible.value = true;
  id.value = value;
};
defineExpose({
  init,
});
const handleClose = () => {
  dialogVisible.value = false;
  ruleFormRef.value.resetFields();
  fileList.value = [];
};

//表单
const ruleForm = reactive({
  name: "",
  desc: "",
});
const rules = reactive({});
const ruleFormRef = ref();
const handleSubmit = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      if (fileList.value.length == 0) {
        ElMessage({
          message: "请上传图片！",
          type: "warning",
        });
        return;
      }
      let params = {
        id: id.value,
        pid: 0,
        level: 1,
        banner: fileList.value[0].url,
      };
      serviceTypeAPI
        .editServiceType(params)
        .then((res) => {
          dialogVisible.value = false;
          emit("updateTerms", pid.value);
          ruleFormRef.value.resetFields();
          fileList.value = [];
        })
        .catch((err) => {});
    } else {
      console.log("error submit!", fields);
    }
  });
};
//上传图片

const fileList = ref([]);

const dialogImageUrl = ref("");
const dialogVisibleImg = ref(false);

const handleRemove = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles);
};

const handlePictureCardPreview = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url;
  dialogVisibleImg.value = true;
};
const handleSuccess = (response, uploadFile, uploadFiles) => {
  uploadFiles.find((item) => item.name == uploadFile.name).url =
    uploadFile.response.data.url;
};
const handleExceed = () => {
  ElMessage({
    message: "最多选择一张图片",
    type: "warning",
  });
};
</script>
<style lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
</style>
